package com.vst.serverdatalib.https;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.conn.params.ConnConnectionParamBean;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.text.TextUtils;

import com.vst.serverdatalib.entity.UrlRequestEntity;

public class HttpHelper {

    public static final int GET = 0x11;

    public static final int POST = GET + 1;

    public static final String COOKIE = "cookie";

    public static final String REFERER = "referer";

    public static final String USER_AGENT = "user_agent";

    private static final String TAG = "HttpHelper";

    public static String getJsonContent(String url) {
        return getJsonContent(url, null, null, GET);
    }

    public static String getJsonContent(UrlRequestEntity entity, UrlRequestEntity defaultEntity) {
        return getJsonContent(null, entity, defaultEntity);
    }

    public static String getJsonContent(ArrayList<NameValuePair> pairs, UrlRequestEntity entity,
            UrlRequestEntity defaultEntity) {
        try {
            HttpEntity httpEntity = getHttpEntity(pairs, entity, defaultEntity);
            if (entity != null) {
                return EntityUtils.toString(httpEntity, "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Header[] getHeaders(UrlRequestEntity entity, UrlRequestEntity defaultEntity) {
        if (entity != null && defaultEntity != null && !TextUtils.isEmpty(entity.getUrl())) {
            Header[] headers = new Header[3];
            String cookie = entity.getCookie();
            if (TextUtils.isEmpty(cookie)) {
                cookie = defaultEntity.getCookie();
            }
            String referer = entity.getReferer();
            if (TextUtils.isEmpty(referer)) {
                referer = defaultEntity.getReferer();
            }
            String userAgent = entity.getUserAgent();
            if (TextUtils.isEmpty(userAgent)) {
                userAgent = defaultEntity.getUserAgent();
            }
            headers[0] = new BasicHeader(COOKIE, cookie);
            headers[1] = new BasicHeader(REFERER, referer);
            headers[2] = new BasicHeader(USER_AGENT, userAgent);
            LogUtil.i(TAG, "entity.mQuestMethod=" + entity.getQuestMethod());
            return headers;
        }
        return null;
    }

    public static HttpEntity getHttpEntity(ArrayList<NameValuePair> pairs, UrlRequestEntity entity,
            UrlRequestEntity defaultEntity) {
        if (entity != null && defaultEntity != null && !TextUtils.isEmpty(entity.getUrl())) {
            LogUtil.i(TAG, "entity.mQuestMethod=" + entity.getQuestMethod());
            return getHttpEntity(entity.getUrl(), getHeaders(entity, defaultEntity), pairs, entity.getQuestMethod());
        }
        return null;
    }

    public static String getJsonContent(String url, int method) {
        return getJsonContent(url, null, null, method);
    }

    public static String getJsonContent(String url, Header[] headers) {
        return getJsonContent(url, headers, null, GET);
    }

    public static String getJsonContent(String url, Header[] headers, int method) {
        return getJsonContent(url, headers, null, method);
    }

    public static String getJsonContent(String url, ArrayList<NameValuePair> pairs) {
        return getJsonContent(url, null, pairs, POST);
    }

    public static String getJsonContent(String url, ArrayList<NameValuePair> pairs, int method) {
        return getJsonContent(url, null, pairs, method);
    }

    public static String getJsonContent(String url, Header[] headers, ArrayList<NameValuePair> pairs) {
        try {
            HttpEntity entity = getHttpEntity(url, headers, pairs, POST);
            if (entity != null) {
                return EntityUtils.toString(entity, "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getJsonContent(String url, Header[] headers, ArrayList<NameValuePair> pairs, int method) {
        try {
            HttpEntity entity = getHttpEntity(url, headers, pairs, method);
            if (entity != null) {
                return EntityUtils.toString(entity, "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getJsonContent(String url, Header[] headers, int method, NameValuePair[] pairs) {
        if (pairs != null) {
            LogUtil.i(TAG, "paris==" + pairs.length);
        }
        ArrayList<NameValuePair> newPairs = null;
        if (pairs != null && pairs.length > 0) {
            newPairs = new ArrayList<NameValuePair>();
            for (int i = 0; i < pairs.length; i++) {
                if (pairs[i] != null) {
                    newPairs.add(pairs[i]);
                }
            }
        }
        return getJsonContent(url, headers, newPairs, method);
    }

    public static HttpEntity getHttpEntity(String url, Header[] headers, ArrayList<NameValuePair> pairs, int method) {
        try {
            if (TextUtils.isEmpty(url)) {
                return null;
            }
            HttpEntity entity = null;
            HttpClient client = new DefaultHttpClient();
            //设置连接超时时间为3s
            client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
            //数据传输时间10s
            client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
            HttpUriRequest request = null;
            switch (method) {
            case GET:
                if (pairs != null && pairs.size() > 1) {
                    url += "?" + URLEncodedUtils.format(pairs, HTTP.UTF_8);
                }
                request = new HttpGet(url);
                break;
            case POST:
                request = new HttpPost(url);
                if (pairs != null && pairs.size() > 1) {
                    LogUtil.i(TAG, "request**********" + pairs);
                    UrlEncodedFormEntity requestEntity = new UrlEncodedFormEntity(pairs, HTTP.UTF_8);
                    ((HttpPost) request).setEntity(requestEntity);
                }
                break;
            }
            if (request == null) {
                return null;
            }
            if (headers != null && headers.length > 1) {
                request.setHeaders(headers);
            }
            LogUtil.d(TAG, "url = " + url);
            HttpResponse response = client.execute(request);
            LogUtil.i(TAG, "responseCode=" + response.getStatusLine().getStatusCode());
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                entity = response.getEntity();
            }
            return entity;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static long getEntityLength(HttpEntity entity) {
        if (entity != null) {
            return entity.getContentLength();
        }
        return -1;
    }

    public static InputStream getStream(HttpEntity entity) {
        if (entity != null) {
            try {
                return entity.getContent();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;

    }

    public static String getJsonContent(UrlRequestEntity entity, UrlRequestEntity urlDefaultEntity, String configPath) {
        LogUtil.i(TAG, "entity=" + entity + ",urlDefaultEntity=" + urlDefaultEntity);
        if (entity != null && urlDefaultEntity != null && !TextUtils.isEmpty(entity.getUrl())) {
            return getJsonContent(entity, urlDefaultEntity);
        }
        return getJsonContent(configPath);
    }
}
